package com.itheima.aop;

import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AOPAdvice {
    @Pointcut("execution(* *(..))")
    //定义方法来通过注解设置切入点方法
    public void pointCut(){

    }
    @Pointcut("execution(* *(int))")
    public void pt(){}

    //通知：增强原始方法(切入点)

    //前置通知对应的方法
    @Before("pointCut()")
    public void open() {
        System.out.println("开启事务");
    }

    @Before(value = "pt()&&args(x)",argNames="x")
    public void before1(int x){
        System.out.println("开启事务....."+x);
    }

    @AfterReturning(value = "pt()",returning="result")
    public void afterTurning(Object result){
        System.out.println("----"+result);
    }


    public void after1(JoinPoint jp){
        try {
            Object[] args = jp.getArgs();
            System.out.println("after1...."+args[0]);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
    //后置通知对应的方法
    @After("pointCut()")
    public void close() {
        System.out.println("关闭事务");
    }

    //返回后通知对应的方法
    @After("pointCut()")
    public void after() {
        System.out.println("关闭事务");
    }

    //通过异常类参数来获取原始方法执行的错误信息
    @AfterThrowing(value = "pointCut()",pointcut = "pointCut()",throwing = "t",argNames = "t")
    public void toThrow(Throwable t) {
        System.out.println(t.getMessage());
    }

    @Around("pointCut()")
    public Object round1(ProceedingJoinPoint pjp) {
        Object proceed = null;
        try {
            proceed = pjp.proceed();
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }




    /*
    JoinPoint：用于前四种通知获取执行目标对象的信息
    ProceedingJoinPoint是JoinPoint子接口
    用于环绕通知获取执行目标对象的信息，特有的方法proceed():继续执行目标方法
     */
    //通过JoinPoint的子接口来调用原始方法
    public void round(ProceedingJoinPoint pjp) {
        try {
            //调用原始方法
            Object o = pjp.proceed();
        } catch (Throwable throwable) {
            System.out.println("1111");
        }
    }




}
